package top.tangyh.lamp.care.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.tangyh.lamp.care.mapper.BPipeConfigMapper;
import top.tangyh.lamp.care.service.BPipeConfigService;
import top.tangyh.basic.base.service.impl.SuperServiceImpl;
import top.tangyh.lamp.care.manager.BPipeConfigManager;
import top.tangyh.lamp.care.entity.BPipeConfig;
import top.tangyh.lamp.care.vo.query.PipeConfigQueryParam;
import top.tangyh.lamp.care.vo.result.PageOutputDTO;
import top.tangyh.lamp.care.vo.result.PipeConfigGroupDTO;
import top.tangyh.lamp.care.vo.result.PipeConfigGroupOutVO;
import top.tangyh.lamp.care.vo.save.BPipeConfigSaveVO;
import top.tangyh.lamp.care.vo.update.BPipeConfigUpdateVO;
import top.tangyh.lamp.care.vo.result.BPipeConfigResultVO;
import top.tangyh.lamp.care.vo.query.BPipeConfigPageQuery;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * 导管配置表
 * </p>
 *
 * @author zqb
 * @date 2024-07-19 15:02:24
 * @create [2024-07-19 15:02:24] [zqb] [代码生成器生成]
 */
@Slf4j
@RequiredArgsConstructor
@Service
@Transactional(readOnly = true)
public class BPipeConfigServiceImpl extends SuperServiceImpl<BPipeConfigManager, Long, BPipeConfig> implements BPipeConfigService {

    private final BPipeConfigMapper bPipeConfigMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BPipeConfigResultVO savePipeConfig(BPipeConfigSaveVO yxPipeConfigVO) {
        BPipeConfig pipeConfigPO = BeanUtil.toBean(yxPipeConfigVO, BPipeConfig.class);
        if (null == pipeConfigPO.getId()) {
            save(pipeConfigPO);
        } else {
            if (null != pipeConfigPO.getId()) {
                BPipeConfig old = this.getById(pipeConfigPO.getId());
                //判断只能本人修改
//                if(null != SecurityUtil.checkRecordAuth(old.getCreateBy() ,old.getUpdateBy())){
//                    throw new BadRequestException(SecurityUtil.checkRecordAuth(old.getCreateBy() ,old.getUpdateBy()));
//                }
            }
            updateById(pipeConfigPO);
        }
        return BeanUtil.toBean(pipeConfigPO, BPipeConfigResultVO.class);
    }

    @Override
    public PageOutputDTO<BPipeConfigResultVO> pagePipeConfig(PipeConfigQueryParam yxPipeConfigQueryParam) {
        IPage<BPipeConfig> data = bPipeConfigMapper.pageYxPipeConfig(yxPipeConfigQueryParam);
        PageOutputDTO<BPipeConfigResultVO> result = new PageOutputDTO<>();
        result.setTotal(data.getTotal());
        result.setCurrent(data.getCurrent());
        result.setPage(data.getPages());
        if (null != data && CollectionUtil.isNotEmpty(data.getRecords())) {
            result.setList(BeanUtil.copyToList(data.getRecords(), BPipeConfigResultVO.class));
        } else {
            result.setList(new ArrayList<>());
        }
        return result;
    }

    @Override
    public List<BPipeConfigResultVO> queryPipeConfig(PipeConfigQueryParam yxPipeConfigQueryParam) {
        List<BPipeConfig> data = bPipeConfigMapper.queryYxPipeConfig(yxPipeConfigQueryParam);
        List<BPipeConfigResultVO> result = new ArrayList<>();
        if (null != data && CollectionUtil.isNotEmpty(data)) {
            result = BeanUtil.copyToList(data, BPipeConfigResultVO.class);
        }
        return result;
    }

    @Override
    public BPipeConfigResultVO getPipeConfigOne(Long id) {
        BPipeConfig pipeConfigPO = bPipeConfigMapper.selectById(id);
        if (null != pipeConfigPO) {
            BPipeConfigResultVO out = BeanUtil.toBean(pipeConfigPO, BPipeConfigResultVO.class);
            return out;
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deletePipeConfig(List<Long> ids) {
        if (CollectionUtil.isNotEmpty(ids)) {
            BPipeConfig old = bPipeConfigMapper.selectById(ids.get(0));
            //验证是否本人 删除
//            if(null != SecurityUtil.checkRecordAuth(old.getCreateBy() ,old.getUpdateBy())){
//                throw new BadRequestException(SecurityUtil.checkRecordAuth(old.getCreateBy() ,old.getUpdateBy()));
//            }
        } else {
            return true;
        }
        return removeByIds(ids);
    }

    @Override
    public List<PipeConfigGroupOutVO> queryChannelConfigGroupByPPtype() {
        List<PipeConfigGroupDTO> dtoList = bPipeConfigMapper.queryChannelConfigGroupByPPtype();
        List<PipeConfigGroupOutVO> result = new ArrayList<>();
        for (PipeConfigGroupDTO dto : dtoList) {
            PipeConfigGroupOutVO vo = new PipeConfigGroupOutVO();
            vo.setPpTypeCode(dto.getPpTypeCode());
            vo.setPpTypeName(dto.getPpTypeName());
            if (CollectionUtil.isNotEmpty(dto.getList())) {
                List<BPipeConfigResultVO> list = BeanUtil.copyToList(dto.getList(), BPipeConfigResultVO.class);
                vo.setList(list);
            }
            result.add(vo);
        }
        return result;
    }
}


